<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="接口名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入接口名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="接口方式" prop="interfaceType">
        <el-select v-model="queryParams.interfaceType" placeholder="请选择接口方式" clearable>
          <el-option v-for="dict in interface_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:gather:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:gather:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:gather:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary"  icon="Refresh" plain @click="handleReset">恢复</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:gather:export']">导出</el-button>
      </el-col> -->
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gatherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="接口名称" align="center" prop="deviceName" />
      <el-table-column label="入参" align="center" prop="param" />
      <el-table-column label="接口方式" align="center" prop="interfaceType">
        <template #default="scope">
          <dict-tag :options="interface_type" :value="scope.row.interfaceType" />
        </template>
      </el-table-column>
      <el-table-column label="采集信息" align="center" prop="gatherInfo" />
      <el-table-column label="数据规则" align="center" prop="dataRule">
        <template #default="scope">
          <dict-tag :options="data_check" :value="scope.row.dataRule" />
        </template>
      </el-table-column>
      <el-table-column label="逻辑规则" align="center" prop="logicRule">
        <template #default="scope">
          <dict-tag :options="logic_check" :value="scope.row.logicRule" />
        </template>
      </el-table-column>
      <el-table-column label="恢复次数" align="center" prop="restore" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:gather:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:gather:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备采集接口维护对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="gatherRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接口名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入接口名称" />
        </el-form-item>
        <el-form-item label="入参" prop="param">
          <el-input v-model="form.param" placeholder="请输入接口名称" />
        </el-form-item>
        <el-form-item label="接口方式" prop="interfaceType">
          <el-select v-model="form.interfaceType" placeholder="请选择接口方式">
            <el-option v-for="dict in interface_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采集信息" prop="gatherInfo">
          <el-input v-model="form.gatherInfo" placeholder="请输入采集信息" />
        </el-form-item>
        <el-form-item label="数据规则" prop="dataRule">
          <el-select v-model="form.dataRule" placeholder="请选择数据规则">
            <el-option v-for="dict in data_check" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逻辑规则" prop="logicRule">
          <el-select v-model="form.logicRule" placeholder="请选择逻辑规则">
            <el-option v-for="dict in logic_check" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Gather">
import { listGather, getGather, delGather, addGather, updateGather, resetGather } from "@/api/system/gather";

const { proxy } = getCurrentInstance();
const { interface_type,data_check,logic_check } = proxy.useDict('interface_type','data_check','logic_check');

const gatherList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceName: null,
    interfaceType: null,
  },
  rules: {
    deviceName: [
      { required: true, message: "接口名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备采集接口维护列表 */
function getList() {
  loading.value = true;
  listGather(queryParams.value).then(response => {
    gatherList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deviceName: null,
    interfaceType: null,
    gatherInfo: null,
    dataRule: null,
    logicRule: null,
    param: null,
    restore: null
  };
  proxy.resetForm("gatherRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备采集接口维护";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getGather(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备采集接口维护";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["gatherRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateGather(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGather(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/* 恢复操作 */
function handleReset(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认恢复设备采集接口维护编号为"' + _ids + '"的数据项？').then(function () {
    return resetGather(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("恢复成功");
  }).catch(() => { });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除设备采集接口维护编号为"' + _ids + '"的数据项？').then(function () {
    return delGather(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/gather/export', {
    ...queryParams.value
  }, `gather_${new Date().getTime()}.xlsx`)
}

getList();
</script>
